package webservices;


import java.util.List;
import dao.DAOFactory;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET; 
import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path; 
import javax.ws.rs.PathParam; 
import javax.ws.rs.Produces; 
import javax.ws.rs.core.Context;
import javax.ws.rs.core.GenericEntity;
import javax.ws.rs.core.MediaType; 
import javax.ws.rs.core.Request;
import javax.ws.rs.core.UriInfo;

import sun.security.krb5.Credentials;
import classes.Market;


@Path("/marketi")
public class MarketResource {
	 @Context
	 UriInfo uriInfo;
	 @Context
	 Request request;
	 
	/** 
	 @GET
	 @Path("login")
	 @Produces(MediaType.APPLICATION_XML)
	 public Market prijaviNaSistem(@HeaderParam("username")String username, @HeaderParam("password")String password) throws Exception{
		 try{
			 DAOFactory.connect();
			 String naziv = MarketDAO.nazivMarketa(username)
			 DAOFactory.disconnect();
			 return k;
		 }
		 catch(Exception e){
			 e.printStackTrace();
			 return null;
		 }
	  }*/
	 
	 
	 	@Path("/market/{id}")
		@GET
		@Produces(MediaType.APPLICATION_XML)
		public Market vratiMarket(@PathParam("id")String sIDMarketa) {
			try {
				int idMarketa = Integer.parseInt(sIDMarketa);
				DAOFactory.connect();
				Market m = DAOFactory.getMarketDAO().selectMarket(idMarketa);
				DAOFactory.disconnect();
				return m;
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return null;
			}
		}
	 	
	    @Path("/svi")
		@GET
		@Produces(MediaType.APPLICATION_XML)
		public GenericEntity<List<Market>> vratiMarkete() {
			try {
				DAOFactory.connect();
				List<Market> marketi = DAOFactory.getMarketDAO().selectSviMarketi();
				DAOFactory.disconnect();
				return new GenericEntity<List<Market>>(marketi){};
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return null;
			}
		}
	
	 @PUT
	  @Path("market/novi")
	  @Consumes(MediaType.APPLICATION_XML)
	  @Produces(MediaType.APPLICATION_XML)
	  public String putMarket(@HeaderParam("username")String username, @HeaderParam("password")String password, Market m) throws Exception {
		  try{
			  DAOFactory.connect();
			  DAOFactory.getMarketDAO().insertMarket(m);
			  DAOFactory.disconnect();
			  return Boolean.toString(true);
		  }
		  catch(Exception e){
			  e.printStackTrace();
			  return Boolean.toString(false);
		  }
	  }
	 
	  @POST
	  @Path("market/obrisi")
	  @Consumes(MediaType.APPLICATION_XML)
	  @Produces(MediaType.APPLICATION_XML)
	  public String deleteMarketPOST(@HeaderParam("username")String username, @HeaderParam("password")String password, String sIDMarketa){
		  try{
			  int idMarketa = Integer.parseInt(sIDMarketa);
			  DAOFactory.connect();
			  int i = DAOFactory.getMarketDAO().deleteMarket(idMarketa);
			  if (i!=1){
				  DAOFactory.disconnect();
				  return Boolean.toString(false);
			  }
			  DAOFactory.disconnect();
			  return Boolean.toString(true);
		  }
		  catch(Exception e){
			  e.printStackTrace();
			  return Boolean.toString(false);
		  }
	  }
	 
	 @DELETE
	  @Path("market/obrisi")
	  @Consumes(MediaType.APPLICATION_XML)
	  @Produces(MediaType.APPLICATION_XML)
	  public void deleteMarket(@HeaderParam("username")String username, @HeaderParam("password")String password, @HeaderParam("id")String sIDMarketa){
		  try{
			  int idMarketa = Integer.parseInt(sIDMarketa);
			  DAOFactory.connect();
			  int i = DAOFactory.getMarketDAO().deleteMarket(idMarketa);
			  if (i!=1){
				  DAOFactory.disconnect();
			  }
			  DAOFactory.disconnect();
			/**  int idKorisnikaAdmina = KontrolerPristupa.getID(username, password, Korisnik.Privilegije.Administrator);
			  if (idKorisnikaAdmina!=-1){ 
				  int i = DAOFactory.getKorisnikDAO().deleteKorisnik(idKorisnika);
				  if (i!=1){
					  DAOFactory.disconnect();
					  return Boolean.toString(false);
				  }
				  Logger.ZabiljeziAkciju(idKorisnikaAdmina, "Obrisao korisnika sa ID: "+sIDKorisnika);
				  DAOFactory.disconnect();
				  return Boolean.toString(true);
			  }
			  return Boolean.toString(false);*/
		  }
		  catch(Exception e){
			  e.printStackTrace();
		  }
	  }
 }
